<?php
#[管理系统组]
$sysnav = "cate";

if($sysact != "list")
{
	#[判断权限]
	if($_SESSION["admin"]["typer"] != "system")
	{
		Error("对不起，您没有权限操作当前功能",$mainlink."?file=index");
	}
}
if($sysgroupid)
{
	$rs = $DB->qgGetOne("SELECT groupname FROM ".$prefix."sysgroup WHERE id=".$sysgroupid."");
	$groupname = $rs["groupname"];
	unset($rs);
}
#[系统组管理]
if($sysact == "add")
{
	Foot("sysgroup.add.qg");
}
elseif($sysact == "addok")
{
	$msg = $STR->safe($_POST);
	if(!$msg["groupname"])
	{
		Error("组名称不能为空",$mainlink."?file=sysgroup&act=add");
	}
	if(!$msg["sign"])
	{
		Error("标识符不能为空",$mainlink."?file=sysgroup&act=add");
	}
	#[检测组名称是否存在]
	$sql = "SELECT groupname FROM ".$prefix."sysgroup WHERE groupname='".$msg["groupname"]."'";
	$chkgroup = $DB->qgGetOne($sql);
	if($chkgroup)
	{
		Error("组名称已经被使用",$mainlink."?file=sysgroup&act=add");
	}
	#[检测组标识是否存在]
	$msg["sign"] = strtolower($msg["sign"]);
	$sql = "SELECT sign FROM ".$prefix."sysgroup WHERE sign='".$msg["sign"]."'";
	$chksign = $DB->qgGetOne($sql);
	if($chksign)
	{
		Error("组标识已经被使用",$mainlink."?file=sysgroup&act=add");
	}
	
	$sql = "INSERT INTO ".$prefix."sysgroup(groupname,taxis,sign,tplindex,tpllist,tplmsg,is_thumb,is_tplfile,is_tags,is_album,is_chain,width,height) VALUES";
	$sql.= "('".$msg["groupname"]."','".$msg["taxis"]."','".$msg["sign"]."','".$msg["tplindex"]."',";
	$sql.= "'".$msg["tpllist"]."','".$msg["tplmsg"]."','".$msg["is_thumb"]."','".$msg["is_tplfile"]."','".$msg["is_tags"]."','".$msg["is_album"]."','".$msg["is_chain"]."','".$msg["width"]."','".$msg["height"]."')";
	$insert_id = $DB->qgInsert($sql);
	#[同时新建分类]
	$cate_sign = ucfirst($msg["sign"]);
	$sql = "INSERT INTO ".$prefix."category(sysgroupid,catename,catename_en,taxis,language,psize) VALUES('".$insert_id."','".$msg["groupname"]."','".$cate_sign."','1','".$language."','0')";
	$insert_id = $DB->qgInsert($sql);
	Error("系统组创建成功！",$mainlink."?file=sysgroup&act=list");
}
elseif($sysAct == "orderlist")
{
	#[显示列表信息]
	$rslist = $DB->qgGetAll("SELECT id,groupname,taxis FROM ".$prefix."sysgroup order by taxis asc");
	$msglist = $rslist;
	
	$query = "SELECT * FROM ".$prefix."sysgroup WHERE 1";
	$result=mysql_query($query);
	$number=mysql_num_rows($result);
	
	$ordermsglist = $DB->qgGetAll("SELECT id,groupname FROM ".$prefix."sysgroup WHERE 1 order by taxis asc");
	
	Foot("sysgrouporder.list.qg");
}
elseif($sysAct == "orderbyok")
{
	$i=0;
	$listlayer = substr($listlayer,0,(strlen($listlayer)-1));	

	foreach(explode("=",$listlayer) as $updownlist)
	{
		$i=$i+1;
		$sql="UPDATE ".$prefix."sysgroup SET taxis='".$i."' WHERE id='".$updownlist."'";
		
		$DB->qgQuery($sql);
	}	
	Error("系统组排序成功...",$mainlink."?file=sysgroup&act=orderlist");
}
elseif($sysact == "list")
{
	require_once("class/unlimited_category.class.php");
	#[显示列表]
	$rslist = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup ORDER BY taxis asc LIMIT 0,100");
	$i = 0;
	foreach($rslist AS $key=>$value)
	{
		$CT[$i] = new Category();
		$rscate = $DB->qgGetAll("SELECT * FROM ".$prefix."category WHERE sysgroupid='".$value["id"]."' AND language='".$language."' order by taxis asc");
		foreach($rscate AS $key2=>$value2)
		{
			if(!$value2["catename"] && $value2["catename_en"])
			{
				$value2["catename"] = $value2["catename_en"];
			}
			if(!$value2["catename"] && $value2["catename_ft"])
			{
				$value2["catename"] = $value2["catename_ft"];
			}
			if($value2["catename"] == $value2["catename_en"])
			{
				unset($value2["catename_en"]);
			}
			if($value2["catename"] == $value2["catename_ft"])
			{
				unset($value2["catename_ft"]);
			}
			$rscate[$key2] = $value2;
		}
		$catelist = $CT[$i]->arraySet($rscate,0,"└─","6");
		$value["soncate"] = $catelist;
		$value["sonnum"] = count($catelist);
		$rslist[$key] = $value;
		unset($CT[$i],$rscate,$catelist);
		$i++;
	}
	Foot("sysgroup.list.qg");
}
elseif($sysact == "delete")
{
	$id = intval($id);
	if(!$id)
	{
		Error("操作非法",$mainlink."?file=sysgroup&act=list");
	}
	$sql = "DELETE FROM ".$prefix."sysgroup WHERE id='".$id."'";
	$DB->qgQuery($sql);
	$sql = "UPDATE ".$prefix."category SET sysgroupid='0' WHERE sysgroupid='".$id."'";
	$DB->qgQuery($sql);
	
	#[删除字段数据]
	$rslist = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$id."'");
	foreach($rslist AS $key=>$value)
	{
		$sql = "DELETE FROM ".$prefix."msg_field WHERE fieldid='".$value["id"]."'";
		$DB->qgQuery($sql);
	}
	#[删除字段]
	$sql = "DELETE FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$id."'";
	$DB->qgQuery($sql);
	
	Error("组信息已删除，原组内容已清空，请记得清空相关缓存",$mainlink."?file=sysgroup&act=list");
}
elseif($sysact == "modify")
{
	$id = intval($id);
	if(!$id)
	{
		Error("无法获取的ID号信息",$mainlink."?file=sysgroup&act=list");
	}
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup WHERE id='".$id."'");
	Foot("sysgroup.modify.qg");
}
elseif($sysAct == "modifyok")
{
	$id = intval($id);
	if(!$id)
	{
		Error("无法获取的ID号信息",$mainlink."?file=sysgroup&act=list");
	}
	$msg = $STR->safe($_POST);
	if(!$msg["sign"])
	{
		Error("标识符不能为空",$mainlink."?file=sysgroup&act=modify&id=".$id);
	}
	$msg["sign"] = strtolower($msg["sign"]);
	$sql = "SELECT sign FROM ".$prefix."sysgroup WHERE sign='".$msg["sign"]."' AND id!='".$id."'";
	$chksign = $DB->qgGetOne($sql);
	if($chksign)
	{
		Error("组标识已经被使用",$mainlink."?file=sysgroup&act=modify&id=".$id);
	}
	
	$sql = "UPDATE ".$prefix."sysgroup SET groupname='".$msg["groupname"]."',is_album='".$msg["is_album"]."',is_chain='".$msg["is_chain"]."',taxis='".$msg["taxis"]."',sign='".$msg["sign"]."',";
	$sql.= "tplindex='".$msg["tplindex"]."',tpllist='".$msg["tpllist"]."',tplmsg='".$msg["tplmsg"]."',";
	$sql.= "is_thumb='".$msg["is_thumb"]."',is_tplfile='".$msg["is_tplfile"]."',is_tags='".$msg["is_tags"]."',width='".$msg["width"]."',height='".$msg["height"]."' WHERE id='".$id."'";
	$DB->qgQuery($sql);
	Error("系统组信息编辑成功...",$mainlink."?file=sysgroup&act=list");
}
elseif($sysAct == "fieldgroup")
{
	$sql = "SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE sysgroupid=".$sysgroupid." ORDER BY id ASC";
	$rslist = $DB->qgGetAll($sql);
	$i = 1;
	foreach($rslist AS $key=>$value)
	{
		$fieldlist = explode(",",$value["fieldlist"]);
		$value["field_count"] = count($fieldlist);
		$value["row_index"] = $i;
		$i++;
		$rowlist[] = $value;
	}
	
	Foot("sysgroup.fieldgroup.qg");
}
elseif($sysAct == "fieldgroupadd")
{
	$field_count = $DB->qg_count("SELECT count(*) FROM ".$prefix."sysgroup_field WHERE sysgroupid='".$sysgroupid."'");
	if(!$field_count)
	{
		Error("请先添加字段后操作",$mainlink."?file=sysgroup&act=fieldadd&sysgroupid=".$sysgroupid);
	}
	//所有字段
	$field = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." ORDER BY sortid ASC,id ASC");
	
	Foot("sysgroup.fieldgroupset.qg");
}
elseif($sysAct == "fieldgroupmodify")
{
	$rs = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup_fieldgroup WHERE id=".$id."");
	//所有字段
	$field = $DB->qgGetAll("SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." ORDER BY sortid ASC,id ASC");
	if($rs["fieldlist"])
	{
		$fieldlist_arr = explode(",",$rs["fieldlist"]);
		//包含字段
		foreach($fieldlist_arr AS $key=>$value)
		{
			$rsc = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup_field WHERE id='".$value."'");
			$field_sel[] = $rsc;
		}
		
		foreach($field AS $key=>$value)
		{
			if(in_array($value["id"],$fieldlist_arr))
			{
				$value["selected"] = true;
			}
			$field[$key] = $value;
		}
	}
	Foot("sysgroup.fieldgroupset.qg");
}
elseif($sysAct == "fieldgroupsetok")
{
	$listlayer = substr($listlayer,0,(strlen($listlayer)-1));
	$msg = $STR->safe($_POST);
	$updata["groupname"] = $msg["groupname"];
	$updata["fieldlist"] = $listlayer;
	
	//编辑
	if($id)
	{
		$where["id"] = $id;
		$DB->update("sysgroup_fieldgroup",$updata,$where);
		unset($updata,$where);
		Error("字段值编辑成功",$mainlink."?file=sysgroup&act=fieldgroupmodify&sysgroupid=".$sysgroupid."&id=".$id);
	}
	//添加
	else
	{
		$updata["sysgroupid"] = $sysgroupid;
		$insert = $DB->insert("sysgroup_fieldgroup",$updata);
		unset($updata);
		Error("字段值添加成功",$mainlink."?file=sysgroup&act=fieldgroupmodify&sysgroupid=".$sysgroupid."&id=".$insert);
	}
}
elseif($sysAct == "fieldgroupdel")
{
	$sql = "DELETE FROM ".$prefix."sysgroup_fieldgroup WHERE id='".$id."'";
	$DB->qgQuery($sql);
	
	Error("字段方案删除成功",$mainlink."?file=sysgroup&act=fieldgroup&sysgroupid=".$sysgroupid);
}
elseif($sysAct == "fieldlist")
{
	$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." ORDER BY sortid ASC,id ASC";
	$rslist = $DB->qgGetAll($sql);
	$i = 1;
	foreach($rslist AS $key=>$value)
	{
		if(!$value["field_name"])
		{
			if($value["field_name_en"])
			{
				$value["field_name"] = $value["field_name_en"];
			}
			if($value["field_name_ft"])
			{
				$value["field_name"] = $value["field_name_ft"];
			}
		}
		$value["field_type"] = field_type($value["field_type"]);
		$value["row_index"] = $i;
		$i++;
		$rowlist[] = $value;
	}
	
	$sql = "SELECT * FROM ".$prefix."sysgroup WHERE 1 ORDER BY taxis ASC,id ASC";
	$syslist = $DB->qgGetAll($sql);
	
	Foot("sysgroup.fieldlist.qg");
}
elseif($sysAct == "fieldadd")
{
	if(!$sysgroupid)
	{
		Error("参数错误",$mainlink."?file=sysgroup&act=list");
	}
	
	Foot("sysgroup.fieldset.qg");
}
elseif($sysAct == "fieldmodify")
{
	$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." AND id=".$id."";
	$rs = $DB->qgGetOne($sql);
	if(!$rs)
	{
		Error("字段不存在",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	Foot("sysgroup.fieldset.qg");
}
elseif($sysAct == "fieldsetok")
{
	$msg = $STR->safe($_POST);
	if(!$msg["sign"])
	{
		Error("标识不能为空",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	if(strlen($msg["sign"])<3)
	{
		Error("标识长度不能小于3个字符",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	if(!$msg["field_name"] && !$msg["field_name_en"] && !$msg["field_name_ft"])
	{
		Error("字段名称不能为空",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	if($msg["ifshare"])
	{
		$msg["ifshare"] = 1;
	}
	$id = intval($id);
	$msg["ifshare"] = intval($msg["ifshare"]);
	$msg["sign"] = strtolower($msg["sign"]);
	
	#[系统标识]
	$sysign = array("id","cateid","sortid","allsortid","levelid","additonid","subject","subject_en","subject_ft","style","author","authorid","postdate","modifydate","thumbid","thumb","thumb2","thumb3","thumb4","ufile","ufile1","ufile2","ufile3","ufile4","tplfile","hits","orderdate","istop","isvouch","isbest","isrecommend","meta_title","meta_keywords","meta_description","meta_title_en","meta_keywords_en","meta_description_en","meta_title_ft","meta_keywords_ft","meta_description_ft","ifcheck","content","content_en","content_ft","msgid","sysgroupid","sign","note","groupname","fieldlist","field_name","field_name_en","field_name_ft","field_type","field_option","field_default","ifshare","field_sign","field_content","field_content_en","field_content_ft");
	$syscheck = in_array($msg["sign"],$sysign);
	if($syscheck)
	{
		Error("不允许使用系统标识",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	
	$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." AND sign='".$msg["sign"]."'";
	if($id)
	{
		$sql .= " AND id!=".$id."";
	}
	$check = $DB->qgGetOne($sql);
	if($check)
	{
		Error("该标识已经存在",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	
	if($id)
	{
		$old = $DB->qgGetOne("SELECT * FROM ".$prefix."sysgroup_field WHERE id='".$id."'");
		
		$updata["sign"] = $msg["sign"];
		$updata["field_name"] = $msg["field_name"];
		$updata["field_name_en"] = $msg["field_name_en"];
		$updata["field_name_ft"] = $msg["field_name_ft"];
		$updata["field_type"] = $msg["field_type"];
		$updata["field_option"] = $msg["field_option"];
		$updata["field_default"] = $msg["field_default"];
		$updata["note"] = $msg["note"];
		$updata["ifshare"] = $msg["ifshare"];
		$updata["ifcheck"] = $msg["ifcheck"];
		$DB->update("sysgroup_field",$updata,array("id"=>$id));
		unset($updata);
		
		//如果标示改变了，则修改相应主题字段表的字段标示
		if($old["sign"]!=$msg["sign"])
		{
			$updata["field_sign"] = $msg["sign"];
			$DB->update("msg_field",$updata,array("fieldid"=>$id,"field_sign"=>$msg["sign"]));
		}
		Error("字段修改成功",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	else
	{
		$updata["sysgroupid"] = $sysgroupid;
		$updata["sign"] = $msg["sign"];
		$updata["field_name"] = $msg["field_name"];
		$updata["field_name_en"] = $msg["field_name_en"];
		$updata["field_name_ft"] = $msg["field_name_ft"];
		$updata["field_type"] = $msg["field_type"];
		$updata["field_option"] = $msg["field_option"];
		$updata["field_default"] = $msg["field_default"];
		$updata["note"] = $msg["note"];
		$updata["ifshare"] = $msg["ifshare"];
		$updata["ifcheck"] = $msg["ifcheck"];
		$DB->insert("sysgroup_field",$updata);
		unset($updata);
		Error("字段添加成功",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
}
elseif($sysAct == "fielddel")
{
	#[删除字段]
	$sql = "DELETE FROM ".$prefix."sysgroup_field WHERE id='".$id."'";
	$DB->qgQuery($sql);
	#[删除字段数据]
	$sql = "DELETE FROM ".$prefix."msg_field WHERE fieldid='".$id."'";
	$DB->qgQuery($sql);
	
	Error("字段删除成功",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
}
elseif($sysAct == "fieldorder")
{
	$sql = "SELECT * FROM ".$prefix."sysgroup WHERE 1 ORDER BY taxis ASC,id ASC";
	$syslist = $DB->qgGetAll($sql);
	
	if($sysgroupid)
	{
		$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$sysgroupid." ORDER BY sortid ASC";
		$rslist = $DB->qgGetAll($sql);
		$i = 1;
		foreach($rslist AS $key=>$value)
		{
			$value["row_index"] = $i;
			$i++;
			$msglist[] = $value;
		}
	}
	Foot("sysgroup.fieldorder.qg");
}
elseif($sysAct == "fieldorderok")
{
	$i=0;
	$listlayer = substr($listlayer,0,(strlen($listlayer)-1));	

	foreach(explode("=",$listlayer) as $updownlist)
	{
		$i=$i+1;
		$sql="UPDATE ".$prefix."sysgroup_field SET sortid='".$i."' WHERE id='".$updownlist."'";
		$DB->qgQuery($sql);
	}	
	Error("字段排序成功...",$mainlink."?file=sysgroup&act=fieldorder&sysgroupid=".$sysgroupid);
}
elseif($sysAct == "fieldset")
{
	$myidlist = $STR->safe($idlist);#[获取IDlist]
	if(!$myidlist)
	{
		Error("信息操作不正确",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	$qgtype = $STR->safe($qgtype);
	if($qgtype == "delete")
	{
		$idarray = explode(",",$myidlist);
		foreach($idarray AS $ket=>$value)
		{
			#[删除字段]
			$sql = "DELETE FROM ".$prefix."sysgroup_field WHERE id in(".$myidlist.")";
			$DB->qgQuery($sql);
			#[删除字段数据]
			$sql = "DELETE FROM ".$prefix."msg_field WHERE fieldid in(".$myidlist.")";
			$DB->qgQuery($sql);
		}
		
		Error("批量删除字段完成！",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	elseif($qgtype == "copy")
	{
		$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE id in(".$myidlist.")";
		$rslist = $DB->qgGetAll($sql);
		foreach($rslist AS $key=>$value)
		{
			$copysign[$key] = $value["sign"];
			$copylist[] = $value;
		}
		
		#[判断标识是否重复]
		$copyid = intval($copyid);
		$sql = "SELECT * FROM ".$prefix."sysgroup_field WHERE sysgroupid=".$copyid."";
		$rslist = $DB->qgGetAll($sql);
		if($rslist)
		{
			foreach($rslist AS $key=>$value)
			{
				$hassign[$key] = $value["sign"];
			}
			$result = array_intersect($copysign,$hassign);
			if($result)
			{
				Error("系统标识存在重复",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
			}
		}
		foreach($copylist AS $key=>$value)
		{
			$updata["sysgroupid"] = $copyid;
			$updata["sign"] = $value["sign"];
			$updata["field_name"] = $value["field_name"];
			$updata["field_name_en"] = $value["field_name_en"];
			$updata["field_name_ft"] = $value["field_name_ft"];
			$updata["field_type"] = $value["field_type"];
			$updata["field_option"] = $value["field_option"];
			$updata["field_default"] = $value["field_default"];
			$updata["note"] = $value["note"];
			$updata["ifshare"] = $value["ifshare"];
			$updata["ifcheck"] = $value["ifcheck"];
			$DB->insert("sysgroup_field",$updata);
			unset($updata);
		}
		Error("批量复制字段完成！",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	elseif($qgtype == "ifshare")
	{
		$sql = "UPDATE ".$prefix."sysgroup_field SET ifshare='".$ifshareid."' WHERE id in(".$myidlist.")";
		$DB->qgQuery($sql);
		Error("批量修改字段完成！",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
	else
	{
		$sql = "UPDATE ".$prefix."sysgroup_field SET ";
		switch ($qgtype)
		{
			case "check":
				$sql .= "ifcheck='1'";
				$tmsg = "审核";
			break;
			case "dcheck":
				$sql .= "ifcheck='0'";
				$tmsg = "未审核";
			break;
		}
		$sql .= " WHERE id in(".$myidlist.")";
		$DB->qgQuery($sql);
		Error("批量 <span style='color:red;'>".$tmsg."</span> 操作完成！",$mainlink."?file=sysgroup&act=fieldlist&sysgroupid=".$sysgroupid);
	}
}
?>